var id;
var urlType;
var statusArr = [{ "value": "0", "display": "关闭" }, { "value": "1", "display": "开启" }];
var isRecommendArr = [{ "value": "1", "display": "1" }, { "value": "2", "display": "2" }];

$(function() {
  urlType = ly.getUrlParam("type");
  id = ly.getUrlParam("id");

  // 修改
  if (urlType == 2) {
    $(".title span").text("编辑");
    $(".form").getFormTwo({
      id: id
    }, function(data) {
      $("#status").getSelect({
        type: "radio",
        asName: "status",
        list: statusArr,
        default: data.status
      })
      $("#isRecommend").getSelect({
        type: "radio",
        asName: "isRecommend",
        list: isRecommendArr,
        default: data.isRecommend
      })
    });
    // 新增
  } else {
    $("#status").getSelect({
      type: "radio",
      asName: "status",
      list: statusArr
    })
    $("#isRecommend").getSelect({
      type: "radio",
      asName: "isRecommend",
      list: isRecommendArr
    })
  }

  //提交
  $("body").on("click", "#save-btn", function(event) {
    event.preventDefault();
    var success = $('.form').validate('submitValidate');
    if (success) {
      //验证通过提交表单
      submitFom();
    }
  })

});

/*
 * 提交表单
 */
function submitFom() {
  var title = $("#title").val();
  var coverImg = $('#coverImg .upload-state-done').find('input[type=hidden]').val();
  var status = $("#status").find('input[name=status]:checked').val();

  var obj = {
    'hospitalName': hospitalName,
    'createTime': new Date().getTime()
  };
  var url;
  if (urlType == 1) {
    url = site.apiUrl + "/hospitalManager/addMedicalHospitalInfo"
  } else { //修改
    url = site.apiUrl + "/hospitalManager/updateMedicalHospitalInfo"
    var thisId = $("#currentId").val();
    obj.id = thisId
  }
  request({
    url: url,
    data: obj,
    type: "post",
    ajaxform: 'true',
    callback: function(f) {
      if (f.code == 200) {
        if (urlType == 1) {
          ly.alert('添加成功', function() {
            ly.goto("list.html");
          });
        } else {
          ly.alert('修改成功', function() {
            ly.goto("list.html");
          });
        }
      } else {
        ly.toast(f.desc || f.msg);
      }
    }
  });
}